package  
{
	import flash.display.MovieClip;
	import flash.geom.Point;
	
	public class Piece extends MovieClip
	{
		public static var puzzles:Array = [
											[['water','fire','empty','empty','empty','wind','earth','wind'],
											['fire','rock','empty','empty','wind','empty','rock','earth'],
											['water','fire','rock','fire','earth','rock','empty','wind'],
											['empty','water','fire','water','wind','earth','wind','empty'],
											['empty','fire','rock','earth','fire','water','empty','empty'],
											['earth','rock','wind','rock','water','fire','water','empty'],
											['wind','earth','earth','wind','empty','water','fire','water'],
											['earth', 'wind', 'earth', 'empty', 'empty', 'empty', 'water', 'fire']]
											];
											
	public static var puzzlesSolutions:Array = [
												[['fire', 'earth', 'earth', 'earth', 'earth', 'earth', 'earth', 'earth'],
												['fire', 'rock', 'empty', 'empty', 'empty', 'empty', 'rock', 'water'],
												['fire', 'empty', 'rock', 'empty', 'empty', 'rock', 'empty', 'water'],
												['fire', 'empty', 'empty', 'empty', 'empty', 'empty', 'empty', 'water'],
												['fire', 'empty', 'rock', 'empty', 'empty', 'empty', 'empty', 'water'],
												['fire', 'rock', 'empty', 'rock', 'empty', 'empty', 'empty', 'water'],
												['fire', 'empty', 'empty', 'empty', 'empty', 'empty', 'empty', 'water' ],
												['wind', 'wind', 'wind', 'wind', 'wind', 'wind', 'wind', 'water']]
												];
											
									
		public static var pieceWidth = 38;
		public static var pieceHeight = 37;
		
		public var row:int;
		public var column:int;
		public var currentBoard:Array;
		public var positions:Array;
		
		public function Piece(frame:String, posX:Number, posY:Number, row:int, column:int) 
		{
			this.gotoAndStop(frame);
			this.x = posX;
			this.y = posY;
			
			this.row = row;
			this.column = column;
		}
		
		public static function checkSolution(board:Array, level):Boolean
		{
			var boardSize:int = 8;
			var boardSolution:Array = puzzlesSolutions[level -1];
			
			for (var i:int = 0; i < boardSize; i++)
			{	
				for (var j:int = 0; j < boardSize; j++)
				{
					if (board[i][j].currentLabel != boardSolution[i][j])
						return false;
				}
			}
				
			
			return true;
		}
		
		public function moves(board:Array):Array 
		{
			positions = new Array();
			currentBoard = board;
			
			var steps:int = 7;
			
			down(steps);
			up(steps);
			left(steps);
			right(steps);
			
			return positions;
		}
		
		public function up(steps:int):void
		{
			for(var i:int = (this.row - 1); i >= (this.row - steps); i--)
			{
				if(i < 0)
					break;
				
				positions.push(new Point(i, this.column));
				
				if (currentBoard[i][this.column].currentLabel != "empty")
				{
					break;
				}
			}
		}

		public function down(steps:int):void
		{
			for(var i:int = (this.row + 1); i <= (this.row + steps); i++)
			{
				if(i > 7)
					break;
					
				positions.push(new Point(i, this.column));
				
				if (currentBoard[i][this.column].currentLabel != "empty")
				{
					break;
				}
			}
		}

		public function left(steps:int):void
		{
			for(var j:int = (this.column - 1); j >= (this.column - steps); j--)
			{
				if(j < 0)
					break;
					
				positions.push(new Point(this.row, j));
				
				if (currentBoard[this.row][j].currentLabel != "empty")
				{
					break;
				}
			}
		}
		
		public function right(steps:int):void
		{
			for(var j:int = (this.column + 1); j <= (this.column + steps); j++)
			{
				if(j > 7)
					break;
				
				positions.push(new Point(this.row, j));
				
				if (currentBoard[this.row][j].currentLabel != "empty")
				{
					break;
				}
			}
		}
	}

}